open Format
open Utils.Pervasive
open Utils.Error
open Syntax
open Binding
open PrintExpr
open Core
open GPUtils
open Ctx_visual_model

type context_open_option = AddToCurrent | MakeNew
let coo = ref MakeNew
let openLast = ref None


type ps_window_setting =  OpenOnCreate of string | Hide | OpenFileDlgPath of string 	


  
  
  
let load_context ctx_ref ~(view : GTree.view) psetLabel ps_file = 
	let ctx = parse_file begin
		match !coo with
		| MakeNew ->  empty_context 
		| AddToCurrent -> !ctx_ref end ps_file in
	psetLabel#set_text ps_file;
	(match !openLast with
	|Some fsett -> 
		let oc = open_out fsett in
		output_string oc (Filename.dirname ps_file ^ "\\\n"); close_out oc
	|_->( ));
	bind_ctx_to_view view ctx;
	ctx_ref := ctx
	
	

let file_ok_sel filew ctx_ref ~(view : GTree.view) psetLabel ( ) =
	let ps_file =  filew#filename in
	load_context ctx_ref ~view:view psetLabel ps_file;
	filew#destroy( )
  
let open_from_file  ctx_ref ~(view : GTree.view) psetLabel ?(filename="") ( )= 
	
	let filew = if filename="" 
		then GWindow.file_selection 
			~title:"Select primitive file" 
			~border_width:10  () 
		
		
		else GWindow.file_selection 
			~title:"Select primitive file" 
			~border_width:10 ~filename () in
	un (filew#ok_button#connect#clicked 
			~callback:(file_ok_sel filew ctx_ref ~view psetLabel));
	un (filew#cancel_button#connect#clicked 
			~callback:filew#destroy);
	filew#show()    
    


let show_ps_window ctx_ref psetLabel settings = 
 
  let window = GWindow.window ~title:"Context and initial bindings" () in
		window#set_default_size ~width:850 ~height:400;
		
  let box1 = GPack.hbox ~packing:window#add () in
  let vbox = GPack.vbox ~border_width:8 ~width:500 ~spacing:8 ~packing:box1#add () in
  un(GMisc.label 
		~text:"Current context"
		~packing:vbox#pack ());
  
  let sw = GBin.scrolled_window ~shadow_type:`ETCHED_IN ~hpolicy:`AUTOMATIC
      ~vpolicy:`AUTOMATIC ~packing:vbox#add () in

  let treeview = init_view (GTree.view ~packing:sw#add ( )) in  
  let hbox1 = GPack.vbox ~border_width:8 ~height: 60 ~spacing:8 ~packing:box1#add () in
  let file_select_button = 
		GButton.button ~label:"Select new from file" ~packing:hbox1#pack () in
	
	un ( try 
	match List.find (function  OpenFileDlgPath _->true |_->false) settings 
	with OpenFileDlgPath filename ->
			let filename = 
			(try (let ic = open_in (filename ^ "settings.dat") in 	 
				let res = input_line ic in close_in ic;
				openLast := Some (filename ^ "settings.dat"); res)
				with _ -> filename) in
				
			file_select_button#connect#clicked 
			~callback:(open_from_file ctx_ref ~view:treeview ~filename psetLabel)
		|_->failwith "Unkonw"
		with _-> file_select_button#connect#clicked 
		~callback:(open_from_file ctx_ref ~view:treeview psetLabel));
			
(*	One day: let add_ctx_button = 
		GButton.button ~label:"Add from file" ~packing:hbox1#pack () in*)
		
			
	let test_box = 
		GPack.hbox ~border_width:8 ~height: 60 ~spacing:8 ~packing:hbox1#add () in
	let close_button = 
		GButton.button ~label:"ok" ~packing:hbox1#add () in
		
	begin try let filepath = 
		match List.find (function OpenOnCreate _ ->true |_->false) settings with
		| OpenOnCreate path -> path
		|_->failwith "should never get here" in
		load_context ctx_ref ~view:treeview psetLabel filepath
	with |_-> ( ) end;
	
	un(close_button#connect#clicked ~callback:window#destroy);
	if List.exists (function Hide -> true |_->false)
	settings then	( )	
	else window#show( )
	